/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author 500134
 */
@WebServlet(name = "EditHusServlet", urlPatterns = {"/EditHusServlet"})
public class EditHusServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            int index = 0;
            String husId = request.getParameter("husId");
            int id = Integer.parseInt(husId);
            String navn = request.getParameter("profilnavn");
            String areal = request.getParameter("areal");
            String alder = request.getParameter("alder");
            String energiKlasse = request.getParameter("energiclass");
            String primærvarme = request.getParameter("PHeatsource");
            String sekundærvarme = request.getParameter("SHeatsource");
            String antallgløde = request.getParameter("antall");
            String andelgløde = request.getParameter("andel");
            String[] selected = request.getParameterValues("selected");
            int area = Integer.parseInt(areal);
            int age = Integer.parseInt(alder);
            int antall = Integer.parseInt(antallgløde);
            int andel = Integer.parseInt(andelgløde);
            db.DatabaseKobling.UpdateStructure(id, navn, area, age, energiKlasse, primærvarme, sekundærvarme, antall, andel);
            db.DatabaseKobling.DeleteStructureFurniture(id);
            
            request.getSession().setAttribute("message", "Bygningen ble endret!");
            response.sendRedirect("mineBygninger.jsp");
            List<String> list = new ArrayList<String>();
            list.addAll(Arrays.asList(selected));
            String listString = "";
            for (String s : list) {
                listString += s + " ";
            } 
            String[] newList = listString.split(" ");
            for(int i = 0; i<newList.length/2; i++){
                db.DatabaseKobling.addStructureFurniture(db.DatabaseKobling.getArticleId(newList[index]), id, Integer.parseInt(newList[index+1]));
                index += 2;
            }
            
        } catch(Exception e) {            
            e.printStackTrace();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
